#include "temperature.h"
#include "Motor.h"
#include "app_trace.h"
#include "battery_ctrl.h"
#include "blecom.h"
#include "body_temperature.h"
#include "ct1711.h"
#include "datetime.h"
#include "display_ctrl.h"
#include "display_interface_240x240.h"
#include "font/num_font.h"
#include "heart_rate_interface.h"
#include "lfs_file_manager_auto.h"
#include "math.h"
#include "motor.h"
#include "platform_assert.h"
#include "rtc.h"
#include "sport_his.h"
#include "sports_mode.h"
#include "temperature.inc"
#include "times/view_time_0.h"
#include "times/view_time_11.h"
#include "times/view_time_19.h"
#include "times/view_time_8.h"
#include "times/view_times.h"
#include "view.h"
#include "view_common.h"
#include "weather_ctrl.h"
#define LOG(fmt, ...) //app_trace_log(fmt "\r\n", ##__VA_ARGS__)

typedef struct _temperature_xy
{
	int x;
	int y;
}temperature_xy;

static temperature_xy show_temperature_float(int x, int y, float temperature)
{
		temperature_xy  point = {0};
    const unsigned char *const *image_tab = num_12x29_TAB;
    int const font_width = image_tab[0][0] + (image_tab[0][1] << 8);
    int const font_height = image_tab[0][2] + (image_tab[0][3] << 8);
    int const interval = 3;
    int draw_x = 0;
    if (isnan(temperature))
    {
        x += 30;
        disp_draw_rect_color(x, y + font_height / 2 - 2, x + font_width - 1, y + font_height / 2, RGB565_COLOR_WHITE);
        x += font_width + interval;
        disp_draw_rect_color(x, y + font_height / 2 - 2, x + font_width - 1, y + font_height / 2, RGB565_COLOR_WHITE);
        return point;
    }
    int temperature_int = temperature * 100;
    if (temperature < 0)
    {
        disp_draw_rect_color(x, y + font_height / 2 - 1, x + font_width - 1, y + font_height / 2, RGB565_COLOR_WHITE);
        x += font_width + interval;
    }
    draw_x = disp_draw_user_image_num_filter_color(x, y, temperature_int / 100, font_width + interval, image_tab);
    disp_draw_rect_color(draw_x, y + font_height - 3, draw_x + 3 - 1, y + font_height - 1, RGB565_COLOR_WHITE);
    draw_x += 3 + interval;
    draw_x = disp_draw_user_image_num_with_digits_filter_color(draw_x, y, temperature_int % 100, 2, font_width + interval, image_tab);
    draw_x += 2;
		point.x = draw_x;
		point.y = y;
		return point;
}

void show_temperature_float_width_centigrade(int x, int y, float temperature, bool is_centigrade)
{
		temperature_xy degree =  show_temperature_float(x,y,temperature);
		if(isnan(temperature))
		{
		return;
		}
    if (is_centigrade)
    {
        disp_draw_user_image_filter_color(degree.x, degree.y, icon_centigrade);
    }
    else
    {
        disp_draw_user_image_filter_color(degree.x, degree.y, icon_Fahrenheit);
    }
}


void show_temperature_float_with_degree(int x,int y,float temperature)
{
	temperature_xy degree = show_temperature_float(x,y,temperature);
	if(isnan(temperature))
	{
		return;
	}
	disp_draw_user_image_filter_color(degree.x,degree.y,icon_degree);
}


static void view_temperature_background(void)
{
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, 10, 34, FILE_IMG_Temperature_function);
}

static void view_temperature_beigin_icon(void)
{
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_1, 26, 179, FILE_IMG_Measurement_text5);
}

static void view_temperature_measuring_temperature(void)
{
    disp_draw_user_image(22, 166, img_temperature_wrist_temperature);
    show_temperature_float_width_centigrade(43, 167, disp_ctrl.com_data.f_temp[25], disp_ctrl.com_data.i_temp[21] == WEATHER_TEMPERATURE_TYPE_C);
    disp_draw_user_image(22, 193, img_temperature_body_temperature);
    show_temperature_float_width_centigrade(43, 195, disp_ctrl.com_data.f_temp[26], disp_ctrl.com_data.i_temp[21] == WEATHER_TEMPERATURE_TYPE_C);
}

void view_temperature_base_display(int view)
{
    if (disp_ctrl.loop_cnt == 0)
    {
        disp_ctrl.auto_refresh_time = 1000;
        switch (view)
        {
        case view_temperature_icon:
            break;
        case view_temperature_detail:
            if (disp_ctrl.auto_refresh_count <= BODY_TEMPERATURE_SECOND)
            {
                disp_ctrl.auto_close_delay = 2000 + DISP_CLOSE_TIME_DEFAULT;
            }
            break;
        }
        float temperature = ct1711_get_temperature();
        disp_ctrl.com_data.i_temp[21] = weather_get_temperature_type();
        disp_ctrl.com_data.f_temp[25] = get_wrist_temperature();
        disp_ctrl.com_data.f_temp[26] = get_body_temperature();
        disp_ctrl.com_data.i_temp[5] = ble_if_connected();
        disp_ctrl.com_data.i_temp[0] = get_manual_measure_remain_second();
    }
    show_time_0_ble(disp_ctrl.com_data.i_temp[5]);
    view_temperature_background();
    view_temperature_beigin_icon();
    show_time_0_battery(disp_ctrl.com_data.i_temp[7]);
}

void view_temperature_leveal_1_back_display(int view)
{
    if (disp_ctrl.loop_cnt == 0)
    {
        disp_ctrl.auto_refresh_time = 1000;
        switch (view)
        {
        case view_temperature_icon:
            break;
        case view_temperature_detail:
            if (disp_ctrl.auto_refresh_count <= BODY_TEMPERATURE_SECOND)
            {
                disp_ctrl.auto_close_delay = 2000 + DISP_CLOSE_TIME_DEFAULT;
            }
            break;
        }
        float temperature = ct1711_get_temperature();
        disp_ctrl.com_data.i_temp[21] = weather_get_temperature_type();
        disp_ctrl.com_data.f_temp[25] = get_wrist_temperature();
        disp_ctrl.com_data.f_temp[26] = get_body_temperature();
        disp_ctrl.com_data.i_temp[5] = ble_if_connected();
        disp_ctrl.com_data.i_temp[0] = get_manual_measure_remain_second();
    }
    show_time_0_ble(disp_ctrl.com_data.i_temp[6]);
    show_time_0_battery(disp_ctrl.com_data.i_temp[7]);
    view_temperature_background();
    view_temperature_measuring_temperature();
}

void view_temperature_display(int view)
{
    switch (view)
    {
    case view_temperature_icon:
        view_temperature_base_display(view);
        break;
    case view_temperature_detail:
        view_temperature_leveal_1_back_display(view);
        break;
    default:
        break;
    }
}

view_event_result_t view_temperature_detail_event(event_param_t const *param)
{

    switch (param->event)
    {
    case event_view_entry:
        LOG("view_temperature_event_view_entry");
        start_manual_measure(view_temperature_icon);
        // start_body_measure(BODY_TEMPERATURE_SECOND, true, view_temperature_icon);
        if (param->previous_view == view_prompt_hyperthemia || param->previous_view == view_prompt_not_wear)
        {
            LOG("view_temperature_event_view_entry");
            display_show(view_temperature_icon, DISP_MODE_NORMAL);
        }
        return view_event_result_handled;
    case event_view_exit:
        LOG("view_temperature_event_view_exit");
        // stop_body_measure();
        stop_manual_measure();
        app_cmd_send_ack(SmartBand_temperature_exit, 0);
        return view_event_result_handled;
#ifdef EVENT_LEVEL_UP
    case event_move_level_up:
#endif
#ifdef EVENT_BUTTON_FRONT
    case event_button_front_long_press:
#endif
#ifdef USE_TOUCHPAD
    case BUTTON_EVENT_TP_TAP_LONG:
#endif
        display_show(view_temperature_icon, DISP_MODE_NORMAL);
        return view_event_result_handled;
#ifdef EVENT_BUTTON_FRONT
    case event_button_front_press:
#endif
#ifdef USE_TOUCHPAD
    case BUTTON_EVENT_TP_TAP:
#endif
#ifdef EVENT_PREVIOUS_VIEW
    case event_move_previous_view:
#endif
#ifdef EVENT_NEXT_VIEW
    case event_move_next_view:
#endif
#ifdef EVENT_LEVEL_DOWN
    case event_move_level_down:
#endif
        return view_event_result_handled;
    }
    return view_event_result_not_handled;
}
view_event_result_t view_temperature_icon_event(event_param_t const *param)
{
    switch (param->event)
    {
    case event_view_entry:
        ct1711_start();
        return view_event_result_handled;
    case event_view_exit:
        ct1711_stop();
        return view_event_result_handled;
#ifdef USE_TOUCHPAD
    case BUTTON_EVENT_TP_TAP_LONG:
        display_show(view_temperature_detail, DISP_MODE_NORMAL);
        return view_event_result_handled;
    case BUTTON_EVENT_TP_TAP:
        return view_event_result_handled;
#endif
    }
    return view_event_result_not_handled;
}
view_event_result_t view_temperature_event(int view, event_param_t const *param)
{
    switch (view)
    {
    case view_temperature_detail:
        return view_temperature_detail_event(param);
    case view_temperature_icon:
        return view_temperature_icon_event(param);
    }
    return view_event_result_not_handled;
}
